package com.zhiyou.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou.mapper.HomeworkMapper;
import com.zhiyou.model.Homework;
import com.zhiyou.model.Homework_Notice;
import com.zhiyou.service.HomeworkService;
import com.zhiyou.utils.FTPUtil;


@Service
public class HomeworkServiceImpl implements HomeworkService{

	@Autowired
	HomeworkMapper homeworkMapper;

	@Override
	public List<Homework_Notice> selectByStu_Id(int id, String time) {
		System.out.println(id+":"+time);
		return homeworkMapper.selectByStu_Id(id, time);
	}

	@Override
	public void addHN(Homework_Notice homework_Notice,MultipartFile file) {
		if(file!=null){
			String image = FTPUtil.setImage(file);
			homework_Notice.setHomework_notice_content_url(image);
		}
		homeworkMapper.addHN(homework_Notice);
	}

	@Override
	public Homework_Notice selectByHN_id(int id) {
		return homeworkMapper.selectByHN_id(id);
	}

	@Override
	public void addH(Homework homework,MultipartFile file) {
		if(file!=null){
			String image = FTPUtil.setImage(file);
			homework.setHomework_content_url(image);
		}
		homeworkMapper.addH(homework);
	}

	@Override
	public List<Homework> selectAll(int class_id, int teacher_id, String time) {
		return homeworkMapper.selectAll(class_id, teacher_id, time);
	}

	@Override
	public Homework selectByH_id(int id) {
		return homeworkMapper.selectByH_id(id);
	}

	@Override
	public void update(Homework homework) {
		homeworkMapper.update(homework);
	}

	@Override
	public void deleteHN(int id) {
		Homework_Notice homework_Notice = homeworkMapper.selectByHN_id(id);
		if(homework_Notice.getHomework_notice_content_url()!=null){
			FTPUtil.rmImage(homework_Notice.getHomework_notice_content_url());
		}
		homeworkMapper.deleteHN(id);
	}

	@Override
	public void deleteH(int id) {
		Homework homework = homeworkMapper.selectByH_id(id);
		if(homework.getHomework_content_url()!=null){
			FTPUtil.rmImage(homework.getHomework_content_url());
		}
		homeworkMapper.deleteH(id);
	}

	@Override
	public void deleteHNAll(int[] ids) {
		for (int i : ids) {
			Homework_Notice homework_Notice = homeworkMapper.selectByHN_id(ids[i]);
			if(homework_Notice.getHomework_notice_content_url()!=null){
				FTPUtil.rmImage(homework_Notice.getHomework_notice_content_url());
			}
		}
		homeworkMapper.deleteHNAll(ids);;
	}

	@Override
	public void deleteHAll(int[] ids) {
		for (int i : ids) {
			Homework homework = homeworkMapper.selectByH_id(ids[i]);
			if(homework.getHomework_content_url()!=null){
				FTPUtil.rmImage(homework.getHomework_content_url());
			}
		}
		homeworkMapper.deleteHAll(ids);
	}

}
